﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1
{
	public partial class Game1
	{
		/// <summary>
		/// This is called when the game should draw itself.
		/// </summary>
		/// <param name="gameTime">Provides a snapshot of timing values.</param>
		protected override void Draw(GameTime gameTime)
		{
			GraphicsDevice.Clear(Color.CornflowerBlue);

			// TODO: Add your drawing code here

			//graphics.GraphicsDevice.Clear(Color.Black);

			//GraphicsDevice.RasterizerState = new RasterizerState();

			basicEffect.Projection = gameCamera.Projection;
			basicEffect.View = gameCamera.View;
			//basicEffect.World = gameCamera.View;

			foreach (var pass in basicEffect.CurrentTechnique.Passes)
			{
				pass.Apply();

				device.SetVertexBuffer(groundGrid);
				device.DrawPrimitives(PrimitiveType.LineList, 0, 3);
				//GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, groundGrid, 0, 1);

				device.SetVertexBuffer(vertexBuffer);
				device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

				DrawModel(ground.Model);

			}

			base.Draw(gameTime);
		}

		private void DrawModel(Model model)
		{
			foreach (ModelMesh mesh in model.Meshes)
			{
				foreach (BasicEffect effect in mesh.Effects)
				{
					//effect.VertexColorEnabled = true;
					//effect.EnableDefaultLighting();
					effect.PreferPerPixelLighting = true;
					//effect.World = Matrix.Identity;

					// Use the matrices provided by the game camera
					effect.View = gameCamera.View;
					effect.Projection = gameCamera.Projection;
				}
				mesh.Draw();
			}
		}
	}
}